import os,sys,getopt
from support.sys.schedule import JobSchedule, FilmSchedule
from support.httpd.romsit import *
from support.httpd.moviesit import *
from support.httpd.shellsit import *
from support.httpd.tasksit import *
from threading import Thread
if __name__ == "__main__":
    opts, arg = getopt.getopt(sys.argv[1:], 'm:u:p:', ["mediadir=","romdir=","dbhost=", "rootpwd="])
    MOVIE_PORT = 10001
    ROM_PORT = 10002
    WSS_PORT = 10003
    TASK_PORT = 10004
    BILI_PORT = 10005
    romdir = os.path.dirname(os.path.realpath(__file__))
    params = {}
    runmode = ""
    username = ""
    passwd = ""
    rootpwd = ""
    dbhost = "localhost"
    for name, value in opts:
        if(name == "-m"):runmode = value.upper()
        if(name == "-u"):username = value
        if(name == "-p"):passwd = value
        if(name == "--romdir"):params["romdir"] = value
        if(name == "--mediadir"):params["mediadir"] = value
        if(name == "--dbhost"):dbhost = value
        if(name == "--rootpwd"):rootpwd = value
    jobSchedule = JobSchedule(username, passwd, dbhost)

    threadLst = []
    threadLst.append(Thread(target=TaskHttpServer.run, args=[TASK_PORT, jobSchedule]))

    for mode in runmode.split(","):
        if(mode.upper().startswith("WSS")):threadLst.append(Thread(target=ShellServer.run, args=[WSS_PORT]))
        if(mode.upper().startswith("MOVIE")):
            threadLst.append(Thread(target=FilmSchedule.run, args=[params["mediadir"]]))
            threadLst.append(Thread(target=MovieHttpServer.run, args=[MOVIE_PORT]))
        if(mode.upper().startswith("GBA")):threadLst.append(Thread(target=RomHttpServer.run, args=[ROM_PORT, params["romdir"]]))
    print("start")
    for server in threadLst:
        server.start()
    jobSchedule.start(rootpwd)
